package com.sbwebsocket.core.config;

import com.sbwebsocket.core.service.MasterServerWork;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 延迟消息工作线程池初始化
 * @Auther: GSJ
 * @Date: 2022/2/17
 * @Description:
 * @version: 1.0
 */
//@Component
//@Order(999)
public class WsBootStrapStart implements ApplicationRunner {
    @Autowired
    private MasterServerWork masterServerWork;

    @Autowired
    private ScheduledExecutorService threadPoolExecutor;

    @Autowired
    private WsAppConfig appConfig;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //启动master注册
        threadPoolExecutor.scheduleWithFixedDelay(()->{
            try {
                masterServerWork.work();
            }catch (Exception e){
                e.printStackTrace();
            }
        },5000,appConfig.getMasterLeaseTime(), TimeUnit.MILLISECONDS);
    }


//    public static void main(String[] args) {
//        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(r->{
//            return new Thread(r);
//        });
//
//        executorService.scheduleWithFixedDelay(()->{
//            System.out.println("1111");
//        },1000,5000,TimeUnit.MILLISECONDS);
//    }

}
